package com.dadie.dorm.dao.impl;


import com.dadie.dorm.dao.IStudentDao;
import com.dadie.dorm.pojo.Student;
import com.dadie.dorm.pojo.query.StudentQuery;
import com.dadie.dorm.util.JDBCUtil;
import sun.management.Agent;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements IStudentDao {
    @Override
    public List<Student> selectByPage(StudentQuery studentQuery) {
        int offset = (studentQuery.getPage() - 1) * studentQuery.getLimit();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Student> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            // SELECT id,name,password,age,gender FROM student where name like ? and gender=? limit ?,?
            // SELECT id,name,password,age,gender FROM student where name like ? limit ?,?
            // SELECT id,name,password,age,gender FROM student where gender=? limit ?,?
            // SELECT id,name,password,age,gender FROM student limit ?,?
            //你有这个条件，我们就拼上这个条件 and ...， 但是第一个又没有and
            // String sql = "select * from student where 1=1 "
            // if(name is not null) {
            //      sql += "and name like ?"
            // }
            // if(gender is not null) {
            //      sql += "and gender=?"
            // }
            String sql = "SELECT id,name,age,gender,major,banji,dormroom,dormbuild_id FROM student where 1=1 ";
            //这里面放所有搜索条件
            List<Object> queryList = new ArrayList<>();
            String queryName = studentQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add("%" + queryName + "%");
            }
            String queryMajor=studentQuery.getMajor();

            if (queryMajor != null && !"".equals(queryMajor)) {
                sql += " and major like ? ";
                queryList.add("%" + queryMajor + "%");
            }
            sql += " limit ?,?";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            preparedStatement.setInt(queryList.size() + 1, offset);
            preparedStatement.setInt(queryList.size() + 2, studentQuery.getLimit());

            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String age = resultSet.getString("age");
                String gender=resultSet.getString("gender");
                String major=resultSet.getString("major");
                String banji=resultSet.getString("banji");
                String dormroom=resultSet.getString("dormroom");
                String dormbuildId=resultSet.getString("dormbuild_id");


                Student student = new Student(id, name, age, gender, major, banji, dormroom, dormbuildId);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public Integer selectTotalCount(StudentQuery studentQuery) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from student where 1=1 ";
            List<Object> queryList = new ArrayList<>();
            String queryName = studentQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add(queryName);
            }
            String queryMajor=studentQuery.getMajor();

            if (queryMajor!= null && !"".equals(queryMajor)) {
                sql += " and major like ? ";
                queryList.add("%" + queryMajor + "%");
            }
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                totalCount = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalCount;
    }

    @Override
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from student where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public int add(Student student) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into student(name,age,gender,major,banji,dormroom,dormbuild_id) values(?,?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, student.getName());
            preparedStatement.setString(2, student.getAge());
            preparedStatement.setString(3, student.getGender());
            preparedStatement.setString(4, student.getMajor());
            preparedStatement.setString(5, student.getBanji());
            preparedStatement.setString(6, student.getDormroom());
            preparedStatement.setString(7, student.getDormbuildId());

            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public Student getStudentUpdatePage(String id) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        Student student=null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,age,gender,major,banji,dormroom,dormbuild_id from student where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,Integer.parseInt(id));
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                String name = resultSet.getString("name");
                String age = resultSet.getString("age");
                String gender=resultSet.getString("gender");
                String major=resultSet.getString("major");
                String banji=resultSet.getString("banji");
                String dormroom=resultSet.getString("dormroom");
                String dormbuildId=resultSet.getString("dormbuild_id");
                student = new Student(Integer.parseInt(id),name,age,gender,major,banji,dormroom,dormbuildId);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }
        return student;
    }

    @Override
    public Student selectById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Student student = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,age,gender,major,banji,dormroom,dormbuild_id FROM student where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                String name = resultSet.getString("name");
                String age = resultSet.getString("age");
                String gender=resultSet.getString("gender");
                String major=resultSet.getString("major");
                String banji=resultSet.getString("banji");
                String dormroom=resultSet.getString("dormroom");
                String dormbuildId=resultSet.getString("dormbuild_id");
                student = new Student(id, name,age,gender,major,banji,dormroom,dormbuildId);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return student;
    }


    @Override
    public int update(Student student) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count=0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update student set name=?,age=?,gender=?,major=?,banji=?,dormroom=?,dormbuild_id=? where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, student.getName());
            preparedStatement.setString(2, student.getAge());
            preparedStatement.setString(3,student.getGender());
            preparedStatement.setString(4,student.getMajor());
            preparedStatement.setString(5,student.getBanji());
            preparedStatement.setString(6,student.getDormroom());
            preparedStatement.setString(7,student.getDormbuildId());
            preparedStatement.setInt(8,student.getId());
            count=preparedStatement.executeUpdate();
            System.out.println(preparedStatement);
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return count;
    }


}
